package com.taotao.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.search.dao.SearchDao;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@Service
public class SearchServiceImpl implements SearchService {

	@Autowired
	private SearchDao searchDao;

	@Override
	public SearchResult search(String queryString, Integer page, Integer rows) throws SolrServerException {

		SolrQuery query = new SolrQuery();

		query.setQuery(queryString);
		query.setStart((page-1)*rows);
		query.setRows(rows);

		query.set("df", "item_keywords");

		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\\\"color:red\\\">");
		query.setHighlightSimplePost("</em>");
		SearchResult search = searchDao.search(query);

		// 计算查询结果总页数
		long recordCount = search.getRecordCount();
		long pageCount = recordCount / rows;
		if (recordCount % rows > 0) {
			pageCount++;
		}

		search.setPageCount(pageCount);
		search.setCurPage(page);

		return search;
	}

}
